/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FRMListaMovimiento.java
 *
 * Created on 11/10/2012, 08:42:48 PM
 */

package Vistas;

import java.util.Date;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import Service.ServicioMovimiento;
import Beans.Movimiento;
import Hilos.HiloActualizaTablaMovim;
/**
 *
 * @author Enrique Carrion Morales
 */
public class FRMListaMovimiento extends javax.swing.JInternalFrame {

   private MovimTableModel movimientoTableModel;
    public ArrayList<Movimiento> listaMovsMostrar;
     private HiloActualizaTablaMovim hiloActualiza=new HiloActualizaTablaMovim(this);

    /** Creates new form FRMListaMovimiento */
    public FRMListaMovimiento() {
        initComponents();
        listaMovsMostrar=Main.Main.servicioMovimiento.getMovs();
        movimientoTableModel=new MovimTableModel();
         tblMovimientos.setModel(movimientoTableModel);
         hiloActualiza.run();
    }

    public javax.swing.JTextField getTxtFechaIni(){return this.txfechaini;}
    public javax.swing.JTextField getTxtFechaFin(){return this.txfechafin;}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txfechaini = new javax.swing.JTextField();
        txfechafin = new javax.swing.JTextField();
        txtipo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnfiltrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblMovimientos = new javax.swing.JTable();
        btnnuevo = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btnedit1 = new javax.swing.JButton();

        txfechaini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfechainiActionPerformed(evt);
            }
        });

        txfechafin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfechafinActionPerformed(evt);
            }
        });

        jLabel1.setText("Fecha de inicio:");

        jLabel2.setText("Fecha final:");

        jLabel3.setText("Tipo de movimiento:");

        btnfiltrar.setText("Filtrar");
        btnfiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfiltrarActionPerformed(evt);
            }
        });

        tblMovimientos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblMovimientos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMovimientosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblMovimientos);

        btnnuevo.setText("Nuevo");
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });

        btnedit.setText("Editar");

        btnedit1.setText("Eliminar");
        btnedit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnedit1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(btnnuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnedit, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnedit1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txfechafin, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                            .addComponent(txfechaini, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                            .addComponent(txtipo, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))))
                .addGap(32, 32, 32))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnfiltrar)
                .addContainerGap(358, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txfechaini, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txfechafin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnfiltrar)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnedit)
                        .addComponent(btnedit1))
                    .addComponent(btnnuevo, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblMovimientosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMovimientosMouseClicked

}//GEN-LAST:event_tblMovimientosMouseClicked

    private void txfechainiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfechainiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfechainiActionPerformed

    private void txfechafinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfechafinActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfechafinActionPerformed

    private void btnedit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnedit1ActionPerformed
        // TODO add your handling code here:
        
}//GEN-LAST:event_btnedit1ActionPerformed

    private void btnfiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnfiltrarActionPerformed
        // TODO add your handling code that make the filtering:
        Date ini, fin;
        if(this.txfechaini.getText().isEmpty() || this.txfechaini.getText().isEmpty()) return;
        //si no está escrito las fechas, que no haga el filtrado
        ini=new Date( Date.parse(this.txfechaini.getText()) );    // nuevo.setFechaCaducidad(new Date(Date.parse(txfechacad.getText())));
        fin=new Date(Date.parse(this.txfechafin.getText()) );
        ArrayList<Movimiento> resultado, recibido;
        recibido=Main.Main.servicioMovimiento.getMovs();
        resultado=new ArrayList<Movimiento>();
        for(int i=0; i<recibido.size(); i++)
        {
            Movimiento mov=(Movimiento)recibido.get(i);
            Date fecha=mov.getFecha();
            if(fecha.after(ini) && fecha.before(fin) )
                recibido.add(mov);
        }
          this.listaMovsMostrar=resultado;
       
    }//GEN-LAST:event_btnfiltrarActionPerformed

    public void actualizarTabla()
    {
       
        movimientoTableModel.fireTableChanged(null);
    }
    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        // TODO add here code that add new Movement (movimiento):
        FRMMovimiento dlg=new FRMMovimiento();
        dlg.setPadre(this);
        dlg.setVisible(true);
             
    }//GEN-LAST:event_btnnuevoActionPerformed

     class MovimTableModel extends AbstractTableModel{

         public String []NomCol={"ID","fecha","tipo de movimiento", "pedido","cantidad de detalles" };

        @Override
        public int getColumnCount() {
            return NomCol.length;
        }

        @Override
        public int getRowCount() {
            return listaMovsMostrar.size();
        }

        @Override
        public Object getValueAt(int row, int column) {
            Movimiento movim=listaMovsMostrar.get(row);
            Object elem=null;
            switch(column){
                case (0): elem= movim.getId();break;
                case (1): elem= movim.getFecha(); break;
                case (2): 
                    int tm=movim.getTipoMovimiento();
                    String tmov=null;
                    switch (tm)
                    {
                        case 0:
                            tmov="entrada";
                            break;
                        case 1:
                            tmov="salida";
                            break;
                    }
                    elem= tmov;
                    break;
                case (3): elem=movim.getPedido().getId() ; break;
                case (4): 
                    int cantidad=movim.getDetalle().size();
                    elem= cantidad;
                    break;
            }
            return elem;
        }
        public String getColumnName (int col){
            return NomCol[col];
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnedit1;
    private javax.swing.JButton btnfiltrar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblMovimientos;
    private javax.swing.JTextField txfechafin;
    private javax.swing.JTextField txfechaini;
    private javax.swing.JTextField txtipo;
    // End of variables declaration//GEN-END:variables

}
